package com.birdboot.controller;

import com.birdboot.annotation.Controller;
import com.birdboot.annotation.RequestMapping;
import com.birdboot.entity.User;
import com.birdboot.http.HttpServletRequest;
import com.birdboot.http.HttpServletResponse;
import com.birdboot.util.DbUtil;

import java.io.*;
import java.net.URISyntaxException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * 用于处理与用户数据相关业务的类
 */
@Controller
public class UserController {
    private static File userDir;
    static{
        userDir = new File("./users");
        if(!userDir.exists()){
            userDir.mkdirs();
        }
    }
    @RequestMapping("/regUser")
    public void reg(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理用户注册");
        //1获取表单数据
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String nickname = request.getParameter("nickname");
        String ageStr = request.getParameter("age");
        System.out.println(username+","+password+","+nickname+","+ageStr);
        if(username==null||username.isEmpty()||
                password==null||password.isEmpty()||
                nickname==null||nickname.isEmpty()||
                ageStr==null||ageStr.isEmpty()||
                !ageStr.matches("[0-9]{1,3}")){
            //重定向
            response.sendRedirect("/reg_info_error.html");
            return;
        }
        int age = Integer.parseInt(ageStr);
        // 数据库操作

        try {
            //DbUtil.getConnection().var + 回车 + 回车
            Connection connection = DbUtil.getConnection();
            //定义SQL; 创建执行对象; 设置?的值; 执行SQL; 判断结果;
            String sql1 = "SELECT id FROM user WHERE username=?";
            PreparedStatement ps1 = connection.prepareStatement(sql1);
            ps1.setString(1, username);
            ResultSet rs = ps1.executeQuery();
            if (rs.next()){ //用户名被占用
                response.sendRedirect("/have_user.html");
            }else{ //注册流程
                String sql2 = "INSERT INTO user(username,password,nickname,age) VALUES(?,?,?,?)";
                PreparedStatement ps2 = connection.prepareStatement(sql2);
                ps2.setString(1, username);
                ps2.setString(2, password);
                ps2.setString(3, nickname);
                ps2.setInt(4, age);
                int i = ps2.executeUpdate();
                if (i>0){
                    //注册成功
                    response.sendRedirect("/reg_success.html");
                }else{
                    response.sendRedirect("/have_user.html");
                }
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    @RequestMapping("/loginUser")
    public void login(HttpServletRequest request, HttpServletResponse response) {
        System.out.println("开始处理用户登录!!!!");

        //1获取表单信息
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        //必要的验证工作
        if (username == null || username.isEmpty() || password == null || password.isEmpty()) {
            response.sendRedirect("/login_info_error.html");
            return;
        }
        // 数据库操作
        try {
            Connection connection = DbUtil.getConnection();
            String sql = "SELECT id FROM user WHERE username=? AND password=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1, username);
            ps.setString(2, password);
            ResultSet rs = ps.executeQuery();
            if (rs.next()){
                response.sendRedirect("/login_success.html");
            }else{
                response.sendRedirect("/login_fail.html");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
